package tests;

/* Import the UIManager here!!
 *import src.fieldgame.UIManager;
 */

import java.util.Scanner;

import translate.Translate;
import boundary.GUIManager;
import boundary.UI;
import entities.Inventory;
import fields.Ownable;
import fields.Refuge;
import fields.Tax;
import gstd.*;
import fields.*;

import java.lang.*;

public class GameManagerWithoutUI extends GameManagerAbs {

	private int currentPlayerIndex;
	final int START_BALANCE = 10000;
	private Scanner sc = new Scanner(System.in);
	public Player currentPlayer;

	@Override
	public void endMatch() {
		// get winner
		Player winner = getWinner();

		// if player does not select start new match, exit system

	}

	@Override
	public void endRound() {
		// not used
	}

	@Override
	public void initialize() {
		shaker = new Shaker(2, 6);
		Field[] fields = new Field[] {
				new Territory(Translate.print("Tribe Encampment"), 100, 1000),
				new Territory(Translate.print("Crater"), 300, 1500),
				new Tax(Translate.print("Goldmine"), 2000,0),
				new Territory(Translate.print("Mountain"), 500, 2000),
				new Fleet(Translate.print("Second Sail"), 4000),
				new Territory(Translate.print("Cold Dessert"), 700, 3000),
				new Territory(Translate.print("Black Cave"), 1000, 4000),
				new LaborCamp(Translate.print("Huts in the Mountain"), 100, 2500),
				new Territory(Translate.print("The Werewall"), 1300, 4300),
				new Territory(Translate.print("Mountain Village"), 1600, 4750),
				new Fleet(Translate.print("Sea Grover"), 4000),
				new Refuge(Translate.print("Walled City"), 5000),
				new Territory(Translate.print("South Citadel"), 2000, 5000),
				new Territory(Translate.print("Palace Gates"), 2600, 5500),
				new Fleet(Translate.print("The Buccaneers"), 4000),
				new LaborCamp(Translate.print("The Pit"), 100, 4000),
				new Tax(Translate.print("Caravan"), 4000, 0.1),
				new Refuge(Translate.print("Monastery"), 500),
				new Fleet(Translate.print("Privateer Armada"), 4000),
				new Territory(Translate.print("Tower"), 3200, 6000),
				new Territory(Translate.print("Castle"), 4000, 8000)
		};
		gameBoard = new GameBoard(fields);

		// int numberOfPlayers = ui.selectNumberOfPlayers();
		// Player[] players = new Player[numberOfPlayers];
		// for (int i = 0; i < numberOfPlayers; i++) {
		// String name = ui.inputPlayerName(i) + " " + i;
		// players[i] = new Player(name, new Inventory(START_BALANCE));
		// }
		Player[] players = new Player[4];
		for (int i = 0; i < players.length; i++) {
			players[i] = new Player(Translate.print("player %d",i+1), new Inventory(START_BALANCE));
		}

		playerList = new PlayerList(players);
		

	}

	@Override
	public boolean isMatchDone() {
		return true;
	}

	@Override
	public boolean isRoundDone() {
		Player[] players = playerList.getPlayers();
		int playerCounter = 0;
		for (int i = 0; i < players.length; i++) {
			if (players[i].getInventory().getBalance() >= 0)
				playerCounter++;
		}
		this.turnCounter++;
		return playerCounter == 1;
	}

	@Override
	public void match() {
		turnCounter = 0; // remove this later
		Player[] players = playerList.getPlayers();
		for (int i = 0; i < players.length; i++) {
//			players[i].getInventory().setBalance(START_BALANCE);
			players[i] = new Player(players[i].getName(), new Inventory(START_BALANCE));
			((Inventory) players[i].getInventory()).reset();
		}
		

	}

	@Override
	public void round() {
		// not used
	}

	@Override
	public void turn() {

		this.currentPlayer = playerList.getPlayer(currentPlayerIndex);

		// ignore bankrupt players
		if (hasNegativeBalance(currentPlayer)) {
			nextPlayer();
			return;
		}

		// select roll dice, generate dice and get dice sum
		shaker.shake();

		// find the field the player will move to
		int nextFieldIndex = currentPlayer.getfieldIndex() + shaker.getDiceSum();
		nextFieldIndex %= gameBoard.getFields().length;

		// move player to field
		currentPlayer.setFieldIndex(nextFieldIndex);
		Field currentField = gameBoard.getField(nextFieldIndex);

		// if the field is Ownable, has no owner and the player can afford it
		// ask if player wants to buy it
		if (currentField instanceof Ownable) {
			Ownable ownableField = (Ownable) currentField;
			if (ownableField.getOwner() == null && currentPlayer.getInventory().getBalance() >= ownableField.getPrice()) {
//				ownableField.setBuyFieldDecision(ui.askToBuy(ownableField, currentPlayer));
			}
		}
		
		// execute field logic
		currentField.landOnField(currentPlayer);
		
//		//TestScript Implementation
//		if (turnCounter == 4) {
//		currentPlayer.getInventory().addBalance(-100000);
//		}
//		if (turnCounter == 5) {
//		currentPlayer.getInventory().addBalance(-100000);
//		}
//		if (turnCounter == 6) {
//		currentPlayer.getInventory().addBalance(-100000);
//		}
//		
//		Inventory playerOneInventory = (Inventory)getPlayerList().getPlayer(0).getInventory();
//		Inventory playerTwoInventory = (Inventory)getPlayerList().getPlayer(1).getInventory();
//		Inventory playerThreeInventory = (Inventory)getPlayerList().getPlayer(2).getInventory();		
//		Inventory playerFourInventory = (Inventory)getPlayerList().getPlayer(3).getInventory();
//		
//		System.out.println("Player1: " + playerOneInventory.getOwnedFields());
//		System.out.println("Player2: " + playerTwoInventory.getOwnedFields());
//		System.out.println("Player3: " + playerThreeInventory.getOwnedFields());
//		System.out.println("Player4: " + playerFourInventory.getOwnedFields());
		
		// if player is bankrupt, remove ownership from fields, update GUI

		//TestScript Implementation
		


		
		nextPlayer();
		
		
	}

	private boolean hasNegativeBalance(Player currentPlayer) {
		return currentPlayer.getInventory().getBalance() < 0;
	}

	private void nextPlayer() {
		currentPlayerIndex++;
		currentPlayerIndex %= playerList.getPlayers().length;
	}

	private Player getWinner() {
		Player winner = null;
		Player[] players = playerList.getPlayers();
		for (int i = 0; i < players.length; i++)
			if (players[i].getInventory().getBalance() > 0) {
				winner = players[i];
				break;
			}
		return winner;
	}

	int turnCounter; // is reset in endMatch()

	public int getTurnCounter() {
		return turnCounter;
	}
	
	private void helperConsoleText(Player[] players) {
		turnCounter++;
		System.out.println(turnCounter + " isRoundDone");
		for (int i = 0; i < players.length; i++) {
			System.out.println("Player " + (i + 1) + " inventory: " + players[i].getInventory().getBalance() + " total assets: " + ((Inventory) players[i].getInventory()).getTotalAssets()
					+ " number of owned fields: " + ((Inventory) players[i].getInventory()).getOwnedFields().size());
		}
		System.out.println();
	}
	
	public boolean checkBankruptcy(Player currentPlayer){
		if (hasNegativeBalance(currentPlayer)) {
			System.out.println("PLAYER BANKRUPT " + currentPlayer.getName());
			((Inventory) currentPlayer.getInventory()).reset();
			return true;
		}
		return false;
	}
	
	public PlayerList getPlayerList(){
		return this.playerList;
	}
	
	public Player getCurrentPlayer(){
		return currentPlayer;
	}
	
	public Shaker getShaker(){
		return this.shaker;
	}
	
	public GameBoard getGameBoard(){
		return this.gameBoard;
	}
}